package my.dao.lmpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import my.bean.Admin;
import my.dao.IAdminDao;
import my.dbUtil.DBUtils;

public class IAdminDaoImpl implements IAdminDao {
	private Connection conn;

	public IAdminDaoImpl() {
	}

	public IAdminDaoImpl(Connection conn) {
		this.conn = conn;
	}

	@Override
	public int addAdmin(Admin admin) throws SQLException {
		int record = 0;
		PreparedStatement prestmt = null;

		try {
			String sql = "insert into admin values(admin_id.nextval,?,?,?,?,sysdate,0)";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, admin.getAccount());
			prestmt.setString(2, admin.getPassword());
			prestmt.setString(3, admin.getEmail());
			prestmt.setString(4, admin.getPhone());

			record = prestmt.executeUpdate();

			return record;
		} catch (SQLException e) {
			System.err.println("插入失败!");
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int deleteAdminById(int id) throws SQLException {
		int record = 0;
		PreparedStatement prestmt = null;

		try {
			String sql = "delete from admin where admin_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setInt(1, id);

			record = prestmt.executeUpdate();

			return record;
		} catch (SQLException e) {
			System.err.println("删除失败!");
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int deleteAdminByAccount(String account) throws SQLException {
		int record = 0;
		PreparedStatement prestmt = null;

		try {
			String sql = "delete from admin where admin_id=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, account);

			record = prestmt.executeUpdate();

			return record;
		} catch (SQLException e) {
			System.err.println("删除失败!");
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int modifyBaseMessage(Admin admin) throws SQLException {
		int record = 0;
		PreparedStatement prestmt = null;

		try {
			String sql = "update admin set admin_email=?,admin_phone=? where admin_id=?";
			prestmt = conn.prepareStatement(sql);

			prestmt.setString(1, admin.getEmail());
			prestmt.setString(2, admin.getPhone());
			prestmt.setInt(3, admin.getId());

			record = prestmt.executeUpdate();

			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!");
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int modifyAdminPasswordById(int id, String newPassword) throws SQLException {

		int record = 0;
		PreparedStatement prestmt = null;
		try {
			String sql = "update admin set admin_password=? where admin_id=?";
			prestmt = conn.prepareStatement(sql);

			prestmt.setString(1, newPassword);
			prestmt.setInt(2, id);

			record = prestmt.executeUpdate();

			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!");
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public int modifyAdminPrivilegeById(int id, int privilege) throws SQLException {
		int record = 0;
		PreparedStatement prestmt = null;

		try {
			String sql = "update admin set admin_privilege=? where admin_id=?";
			prestmt = conn.prepareStatement(sql);

			prestmt.setInt(1, privilege);
			prestmt.setInt(2, id);

			record = prestmt.executeUpdate();

			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!");
			e.printStackTrace();
			throw e;
		} finally {
			DBUtils.close(null, prestmt, null);
		}
	}

	@Override
	public List<Admin> findAll() {
		List<Admin> list = new ArrayList<>();
		PreparedStatement prestmt = null;
		ResultSet rs = null;

		try {
			String sql = "select * from admin";
			prestmt = conn.prepareStatement(sql);

			rs = prestmt.executeQuery();

			while (rs.next()) {
				int id = rs.getInt(1);
				String account = rs.getString(2);
				String password = rs.getString(3).trim();
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);
				int privilege = rs.getInt(7);

				Admin admin = new Admin(id, account, password, email, phone, regDate, privilege);
				list.add(admin);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return list;
	}

	@Override
	public Admin findById(int id) {
		Admin admin = null;
		PreparedStatement prestmt = null;
		ResultSet rs = null;

		try {
			String sql = "select * from admin where admin_id=?";
			prestmt = conn.prepareStatement(sql);

			rs = prestmt.executeQuery();

			if (rs.next()) {
				String account = rs.getString(2);
				String password = rs.getString(3).trim();
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);
				int privilege = rs.getInt(7);

				admin = new Admin(id, account, password, email, phone, regDate, privilege);
			}
			return admin;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return admin;
	}

	
	@Override
	public Admin findByAccout(String account) {
		Admin admin = null;
		PreparedStatement prestmt = null;
		ResultSet rs = null;

		try {
			String sql = "select * from admin where admin_account like ?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, "%" + account + "%");

			rs = prestmt.executeQuery();

			while (rs.next()) {
				int id = rs.getInt(1);
				String password = rs.getString(3).trim();
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);
				int privilege = rs.getInt(7);

				admin = new Admin(id, account, password, email, phone, regDate, privilege);
			}
			return admin;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return admin;
	}

	@Override
	public Admin findByAccountAndPassword(String account, String password) {
		Admin admin = null;
		PreparedStatement prestmt = null;
		ResultSet rs = null;

		try {
			String sql = "select * from admin where admin_account=? and trim(admin_password)=?";
			prestmt = conn.prepareStatement(sql);
			prestmt.setString(1, account);
			prestmt.setString(2, password);

			rs = prestmt.executeQuery();

			if (rs.next()) {
				int id = rs.getInt(1);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				Timestamp regDate = rs.getTimestamp(6);
				int privilege = rs.getInt(7);

				admin = new Admin(id, account, password, email, phone, regDate, privilege);
			}
			return admin;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtils.close(null, prestmt, rs);
		}
		return admin;
	}

}
